package org.garbagecan.quartzstudy.cron;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class TestMultiRangeCalendar {
	
	public static void main(String[] args) throws Exception {
		test();
	}
	
	public static void test() throws Exception {
		print("\nTestRangeCalendar.test: -------------------------------");

		long[] starts = new long[] {
				parseToDate("2012-04-05 14:00:00.000").getTime(),
				parseToDate("2012-04-05 15:00:00.000").getTime() };
		long[] ends = new long[] {
				parseToDate("2012-04-05 14:10:00.000").getTime(),
				parseToDate("2012-04-05 15:10:00.000").getTime() };

		MultiRangeCalendar rc = new MultiRangeCalendar(starts, ends);

		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler scheduler = sf.getScheduler();
		
		JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("myjob","mygroup").build();
		
		Trigger trigger = TriggerBuilder.newTrigger()
				.withIdentity("mytrigger", "mygroup")
				.forJob(jobDetail)
				.startNow()
				.withPriority(5)
				.modifiedByCalendar("cal")
				.withSchedule(CronScheduleBuilder
					.cronSchedule("0/10 * * * * ? *"))
				.build();
		
		scheduler.addCalendar("cal", rc, true, true);

		scheduler.scheduleJob(jobDetail, trigger);

		scheduler.start();
		Thread.sleep(100000000);
		scheduler.shutdown();
	}

	public static Date parseToDate(String s) throws ParseException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		Date date = df.parse(s);
		return date;
	}

	public static void print(Object obj) {
		System.out.print("\t");
		System.out.println(obj);
	}

}
